package com.sinoglobal.shakeprize.activity.prize;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sinoglobal.shakeprize.R;
import com.sinoglobal.shakeprize.activity.base.AbstractActivity;
import com.sinoglobal.shakeprize.bean.CompleteParamer;
import com.sinoglobal.shakeprize.utils.Contstent;
import com.sinoglobal.shakeprize.utils.OKhttpUtil;
import com.sinoglobal.shakeprize.utils.OKhttpUtil.ResponseCallback;
import com.squareup.picasso.Picasso;

import okhttp3.Request;

/**
 * @author temulu
 * @createdate 2015-10-14 上午11:45:29
 * @description 确认领取方式界面
 */
public class MyPrizeGetTheWay extends AbstractActivity implements
		OnClickListener {
	private static final int NET_FAIL = 1;
	private TextView prizeDetailsStatus, prizeName, prizePrise,
			prizeUsefulLife, prizeusefulinfo, prizeGetInfo, prizeFreightTitle,
			prizeFreightPay;
	private ImageView prizeImg;
	private Button findish;
	private Bundle bundle;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.prize_get_the_way_activity);
		bundle = getIntent().getExtras();
		initView();
	}

	private void initView() {
		titleView.setText("确认领取");
		prizeDetailsStatus = (TextView) findViewById(R.id.prizeDetailsStatus);
		prizeImg = (ImageView) findViewById(R.id.prizeImg);
		prizeName = (TextView) findViewById(R.id.prizeName);
		prizePrise = (TextView) findViewById(R.id.prizePrise);
		prizeUsefulLife = (TextView) findViewById(R.id.prizeUsefulLife);
		prizeusefulinfo = (TextView) findViewById(R.id.prizeusefulinfo);
		prizeGetInfo = (TextView) findViewById(R.id.prizeGetInfo);
		prizeFreightTitle = (TextView) findViewById(R.id.prizeFreightTitle);
		prizeFreightPay = (TextView) findViewById(R.id.prizeFreightPay);
		findish = (Button) findViewById(R.id.prizeReceiveFinish);
		findish.setOnClickListener(this);
		Picasso.with(this).load(Contstent.PICTURE_PATH + bundle.getString("prizepic")).into(prizeImg);
		prizeName.setText(bundle.getString("name"));
		prizePrise.setText(bundle.getString("price"));
		prizeUsefulLife.setText(bundle.getString("acceptenddate"));
		prizeusefulinfo.setText(bundle.getString("remark"));
		switch (bundle.getInt("mode")) {
		case 2:
			prizeGetInfo.setText("收货人:" + bundle.getString("connect_name")
					+ "\n" + bundle.getString("area")
					+ bundle.getString("address") + "\n"
					+ bundle.getString("connect_tel"));
			break;
		case 1:
			prizeFreightTitle.setVisibility(View.GONE);
			prizeFreightPay.setVisibility(View.GONE);
			prizeGetInfo.setText("用户自理");
			break;
		}
	}

	@Override
	public void onClick(View v) {
		int updatePrizeStatus;
		if (v.getId()==R.id.prizeReceiveFinish) {
			if (2 == bundle.getInt("mode")) {
				updatePrizeStatus = 4;
				Map<String, Object> request = new HashMap<String, Object>();
				request.put("userprizeId", bundle.getInt("userprizeId"));
				request.put("linkmen", bundle.getString("connect_name"));
				request.put("linkAddress",
						bundle.getString("area") + bundle.getString("address"));
				request.put("linkPhone", bundle.getString("connect_tel"));
				String json = JSON.toJSONString(request);
				String url = Contstent.PRIZE_PATH
						+ "prize/addUserAddress.do?json=" + json;
				OKhttpUtil.enqueue(url, new ResponseCallback() {

					@Override
					public void onFailure(Request arg0, IOException arg1) {

					}

					@Override
					public void onResponse(String json) throws IOException {
						Log.e("上传地址", json);
						
					}
				});
			} else {
				updatePrizeStatus = 7;
			}
			Map<String, Object> request1 = new HashMap<String, Object>();
			request1.put("userprizeId", bundle.getInt("userprizeId"));
			request1.put("updatePrizeStatus", updatePrizeStatus);
			String json1 = JSON.toJSONString(request1);
			String url1 = Contstent.PRIZE_PATH
					+ "prize/updatePrizeStatusByUId.do?json=" + json1;
			OKhttpUtil.enqueue(url1, new ResponseCallback() {

				private CompleteParamer info;

				@Override
				public void onFailure(Request arg0, IOException arg1) {

				}

				@Override
				public void onResponse(String json) throws IOException {
					try {
						info = JSONObject.parseObject(json,
								CompleteParamer.class);
					} catch (Exception e) {
						Toast.makeText(MyPrizeGetTheWay.this, "访问网络失败，请稍后再试", Toast.LENGTH_SHORT).show();
						return;
					}
					if (100 == info.rsNo) {
						Intent intent = new Intent(mActivity, GetComplete.class);
						startActivity(intent);
						finish();
					}
				}
			});
		}
	}
}
